/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.util;

import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.StrUtil;

/**
 * <b>(VerifyUtil)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/10/26
 */
public class VerifyUtil {

    public static final String DEFAULT_VERIFY_TOKEN_PREFIX = "customer_update_token_";

    public static final long EXPIRE_TIME = 300;

    public static String getTokenCacheKey(String token) {
        return DEFAULT_VERIFY_TOKEN_PREFIX + token;
    }

    public static boolean verify(Object cacheValue, String loginName, String password) {
        // String cacheToken = String.valueOf(cacheValue);
        // return StringUtils.hasText(cacheToken) && JwtUtil.verify(cacheToken, loginName, password);
        return false;
    }

    /**
     * 至少包含一个英文字母
     * 至少包含一个数字
     * 密码长度在7-20个字符
     *
     * @param password String
     *
     * @return boolean
     */
    public static boolean checkPasswordFormat(String password) {
        return StrUtil.isNotBlank(password) && lengthLimit(password) && least1Letter(password) && least1Number(password);
    }

    /**
     * 密码长度在7-20个字符
     *
     * @param password String
     *
     * @return boolean
     */
    public static boolean lengthLimit(String password) {
        return password.length() >= 7 && password.length() <= 20;
    }

    /**
     * 至少包含一个英文字母
     *
     * @param password String
     *
     * @return boolean
     */
    public static boolean least1Letter(String password) {
        for (int i = 0; i < password.length(); i++) {
            if (CharUtil.isLetter(password.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    /**
     * 至少包含一个数字
     *
     * @param password String
     *
     * @return boolean
     */
    public static boolean least1Number(String password) {
        for (int i = 0; i < password.length(); i++) {
            if (CharUtil.isNumber(password.charAt(i))) {
                return true;
            }
        }
        return false;
    }

}